package com.lamatek.tags.google;

import java.io.Serializable;
import java.util.Vector;

import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * GoogleMapBoxTag
 * 
 * This class represents a &lt;googlemaps:box> tag. Developers should not override this
 * class.
 * 
 * @author Tom Cole
 * @version 0.40
 */
public class GoogleMapBoxTag extends TagSupport implements Serializable, GoogleMapEventListener {

    String color = "#ff0000";
    int weight = 2;
    float opacity = 0.50f;
    String pointlist = null;
    Vector points = null;
    Vector events = null;
    /**
     * Overrides doStartTag() in TagSupport. Developers should not override this method.
     */
    public int doStartTag() {
        Tag tag = this;
        while (tag.getParent() != null) {
            if (tag.getParent() instanceof GoogleMapTag) {
                ((GoogleMapTag) tag.getParent()).addBox(this);
                return SKIP_BODY;
            }
            tag = tag.getParent();
        }
        return SKIP_BODY;
    }
    /**
     * Sets the color used (in hexadecimal form) to draw this box.
     * 
     * @param color The color in hexadecimal format.
     */
    public void setColor(String color) {
        this.color = color;
    }
    /**
     * Sets the width of the line used to draw this box.
     * 
     * @param weight The line width in pixels.
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }
    /**
     * Returns the 0-1 based opacity of the line used to draw this box.
     * 
     * @return The opacity of this box from 0 to 1.
     */
    public float getOpacity() {
        return opacity;
    }
    /**
     * Sets the 0-1 based opacity of the line used to draw this box.
     * 
     * @param opacity A float from 0-1.
     */
    public void setOpacity(float opacity) {
        this.opacity = opacity;
    }
    /**
     * Returns the list of points used to draw this box.
     * 
     * @return A comma separated list of point ids.
     */
    public String getPointlist() {
        return pointlist;
    }
    /**
     * Sets the list of points used to draw this box.
     * 
     * @param pointlist A comma separated list of point ids.
     */
    public void setPointlist(String pointlist) {
        this.pointlist = pointlist;
        points = new Vector();
        String[] p = pointlist.split(",");
        for (int i = 0; i < p.length; i++) {
            points.add(p[i].trim());
        }
    }
    /**
     * Returns the number of points used to draw this box.
     * 
     * @return The number of points in this box.
     */
    public int pointCount() {
        if (points == null)
            return 0;
        else
            return points.size();
    }
    /**
     * Returns the points stored at the supplied iteration.
     * 
     * @param p The interator.
     * @return The id of the point at the requested iteration.
     */
    public String getPoint(int p) {
        if (points == null)
            return null;
        else 
            return (String) points.elementAt(p);
    }
    /**
     * Returns the color of the line used to draw this box.
     * 
     * @return The color in hexadecimal format.
     */
    public String getColor() {
        return color;
    }
    /**
     * Returns the weight of the line used to draw this box.
     * 
     * @return The line width in pixels.
     */
    public int getWeight() {
        return weight;
    }
    /**
     * Adds an event listener to this overlay.
     * 
     * @param event A GoogleMapEventTag
     */
    public void addEvent(GoogleMapEventTag event) {
        if (events == null)
            events = new Vector();
        events.add(event);
    }
    /**
     * Returns the list of currently registered event listeners
     * for this overlay.
     * 
     * @return A Vector of GoogleMapEventTags
     */
    public Vector getEvents() {
        return events;
    }
}
